﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.ComponentModel;
using System.ComponentModel;
using System.Text.RegularExpressions;

namespace JHL.SharepointDesignerActions.TextActions
{

    public class Split : Activity
    {
        public static DependencyProperty DelimiterProperty =
            DependencyProperty.Register("Delimiter", typeof(string), typeof(Split));
        [Category("Text Actions"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string Delimiter
        {
            get
            {
                return Convert.ToString(base.GetValue(DelimiterProperty));
            }
            set
            {
                base.SetValue(DelimiterProperty, value);
            }
        }

        public static DependencyProperty InputStringProperty =
            DependencyProperty.Register("InputString", typeof(string), typeof(Split));
        [Category("Text Actions"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string InputString
        {
            get
            {
                return Convert.ToString(base.GetValue(InputStringProperty));
            }
            set
            {
                base.SetValue(InputStringProperty, value);
            }
        }

        public static DependencyProperty ReturnValueProperty =
            DependencyProperty.Register("ReturnValue", typeof(System.Collections.ArrayList), typeof(Split));
        [Category("Text Actions"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public System.Collections.ArrayList ReturnValue
        {
            get
            {
                return (System.Collections.ArrayList)(base.GetValue(ReturnValueProperty));
            }
            set
            {
                base.SetValue(ReturnValueProperty, value);
            }
        }
        

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            
            if (string.IsNullOrEmpty(this.InputString))
            {
                this.ReturnValue = new System.Collections.ArrayList(0);
            }
            else
            {
                string[] splittedString = this.InputString.Split(new string[] { this.Delimiter }, StringSplitOptions.None);
                if (splittedString == null || splittedString.Length == 0)
                    this.ReturnValue = new System.Collections.ArrayList(0);
                else
                {
                    this.ReturnValue = new System.Collections.ArrayList();
                    for(int i = 0; i < splittedString.Length; i++)
                    {
                        this.ReturnValue.Add(splittedString[i]);
                    }
                }
            }

            return ActivityExecutionStatus.Closed;
        }

        private void InitializeComponent()
        {
            // 
            // Split
            // 
            this.Name = "Split";

        }
    }
}
